﻿/*
 * DG (DG Gaming Engine)  - API
 * Copyright (C) 2004  Bob Marks (marksie531@yahoo.com)
 * http://code.google.com/p/dng-gaming
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.dg.gaming.api.common.msg
{
	import nanoxml.XMLElement;
	import com.dg.gaming.api.common.playerstate.*;

	/**
	 * Communication object for informing the server that the state of a
	 * player has been updated.
	 *
	 * @author Bob Marks
	 * @version Alpha 0.2.3
	 */
	public class MsgPlayerState extends MsgTableMessage
	{

		public static const NO_SEAT:int=-1;

		/** Seat number of where the user wishes to sit down at. */
		private var seatNum:int=-1; // defaults to -1 (no seat)
		private var state:PlayerState;

		public static const XML_ATT_STATE:String="state";
		public static const XML_ATT_SEAT:String="seat";

		/**
		 * Constructor which takes an XMLElement.
		 *
		 * @param message  XML version of this method.
		 */
		public function MsgPlayerState(table:String=null, player:Number=-1, seatNum:int=-1, state:PlayerState=null, message:XMLElement=null)
		{
			initUserId(player);
			setTableNum(table);
			this.state=state;
			// Set player state
			if (state == null)
			{
				this.state=new PlayerStateSeated;
			}
			this.seatNum=seatNum;
			if (message != null)
				super(message);

			if (message == null)
			{
				return;
			}


			// Set up fields

			// Set player
			this.state=PlayerStateFactory.getState(message.getStringAttribute(XML_ATT_STATE));
			this.seatNum=message.getIntAttribute(XML_ATT_SEAT, NO_SEAT);
		}

		/**
		 * Return this state.
		 *
		 * @return    State of the player.
		 */
		public function getState():PlayerState
		{
			return this.state;
		}

		/**
		 * Return the seat number.
		 *
		 * @return    Seat number of the player.
		 */
		public function getSeatNum():int
		{
			return this.seatNum;
		}

		/**
		 * Flatten this to an XMLElement.
		 *
		 * @see com.dg.gaming.api.common.msg.ITransmittable#flatten()
		 */
		public override function flatten():XMLElement
		{
			var message:XMLElement=super.flattenWithName(Msg.PLAYER_STATE);
			message.setAttribute(XML_ATT_STATE, state.stringValue());
			if (seatNum != -1)
			{
				message.setIntAttribute(XML_ATT_SEAT, seatNum);
			}

			return message;
		}
	}
}